package com.sxt.opd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.opd.domain.Jiuzhen;
import com.sxt.opd.domain.Patient;
import com.sxt.opd.domain.Record;
import com.sxt.opd.service.JiuzhenService;
import com.sxt.opd.service.PatientService;
import com.sxt.opd.service.RecordService;
import com.sxt.opd.vo.RecordVo;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2020-01-31
 */
@RestController
@RequestMapping("/record")
public class RecordController {


    @Autowired
    private RecordService recordService;
    @Autowired
    private JiuzhenService jiuzhenService;
    @Autowired
    private PatientService patientService;

    /**
     * 前台传日期，后台Date接值类型转换错误
     * @param binder
     * @param request
     */
    @InitBinder
    public void InitBinder(WebDataBinder binder, WebRequest request)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor editor = new CustomDateEditor(df,true);//参数为true表示允许为空值
        binder.registerCustomEditor(Date.class, editor);
    }

    /**
     * 查询
     */
    @RequestMapping("loadAllRecord")
    public DataGridView loadAllRecord(RecordVo recordVo) {
        IPage<Record> page = new Page<>(recordVo.getPage(), recordVo.getLimit());
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(recordVo.getId() != null,"id",recordVo.getId());
        queryWrapper.eq("patientid",recordVo.getPatientid());
        queryWrapper.eq(recordVo.getJiuzhenid() != null,"jiuzhenid",recordVo.getJiuzhenid());
        queryWrapper.orderByDesc("createtime");
        this.recordService.page(page, queryWrapper);
        List<Record> records = page.getRecords();
        for (Record record : records) {
            Patient patient = this.patientService.getById(record.getPatientid());
            record.setPatientname(patient.getPatientname());
        }
        return new DataGridView(page.getTotal(), records);
    }

    /**
     * 添加
     */
    @RequestMapping("addRecord")
    public ResultObj addRecord(RecordVo recordVo) {

        try {
            QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(recordVo.getJiuzhenid()!=null&&recordVo.getJiuzhenid()!=0,"jiuzhenid",recordVo.getJiuzhenid());
            Record record = this.recordService.getOne(queryWrapper);
            if(record !=null){
                return this.updateRecord(recordVo);
            }else{
            Jiuzhen jiuzhen = this.jiuzhenService.getById(recordVo.getJiuzhenid());
            recordVo.setPatientid(jiuzhen.getPatientid());
            recordVo.setCreatetime(new Date());
            this.recordService.save(recordVo);
            return ResultObj.ADD_SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     */
    @RequestMapping("updateRecord")
    public ResultObj updateRecord(RecordVo recordVo) {
        try {
            QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(recordVo.getJiuzhenid() != null,"jiuzhenid",recordVo.getJiuzhenid());
           /* queryWrapper.eq(recordVo.getId() != null,"id",recordVo.getId());*/
            this.recordService.update(recordVo,queryWrapper);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequestMapping("deleteRecord")
    public ResultObj deleteRecord(Integer id) {
        try {
            this.recordService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

